n = int(input())
p_max = 0
if 1000<=n<10000:
    n1, n2, n3, n4 = n//1000, (n//100)%10, (n//10)%10, n%10
    num = [n, n1, n2, n3, n4, n1 * 10 + n2, n2 * 10 + n3, n3 * 10 + n4, n1 * 10 + n3, n1 * 10 + n4, n2 * 10 + n4, n1 * 100 + n2 * 10 + n3, n1 * 100 + n2 * 10 + n4, n1 * 100 + n3 * 10 + n4, n2 * 100 + n3 * 10 + n4]
    for p in range(2, n+1):
        if p in num:
            x = p
            for d in range(2, p):
                if p % d == 0:
                    x = 0
                    break
            p_max = max(p_max, x)
print(p_max)
